import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/common/js/request.js'
Vue.use(Vuex)
//引入全局变量
import GLOBAL from '@/common/js/global.js'


const store = new Vuex.Store({
	state: {
		onMessage: false, //是否有新的消息
		hasLogin: false,
		baseUrl: 'http://122.51.141.95:8080',
		imgUrl: 'http://122.51.141.95:8762/image/',
		wsUrl: 'wss://huoyuchuan.xyz:8889',
		livePushUrl:'rtmp://122.51.141.95:1935/hls/', //直播推流地址
		SLAT: '123456',
		disabled: false,
		retryLoginMax: 5, //一定时间内最大重试登录次数
		retryLoginCur: 1, //当前重试登录次数
		userInfo: {},
		currentRoom:{id:'',onMessage: false}//当前房间
	},
	mutations: {
		setBaseUrl(state, baseUrl) {
			state.baseUrl = baseUrl
		},
		setWsUrl(state, wsUrl) {
			state.wsUrl = wsUrl
		},
		isLogin(state) {
			if (!state.hasLogin) {
				uni.navigateTo({
					url: '/pages/login/login',
				});
			}
		},
		async login(state, data) {
			let res = await request.post("/user-admin/login/loginForApp", {
				data: data
			});
			// 手动修改配置 setConfig方法会影响全局，请谨慎使用
			request.setConfig({
				header: {
					'token': res.token,
					'content-type': 'application/json'
				}
			});
			var userInfo = res.data;
			Vue.set(userInfo, 'plaintext', data.userPass); //正确姿势
			uni.setStorage({
				key: GLOBAL.CacheKeys.token,
				data: res.token
			})
			uni.setStorage({
				key: GLOBAL.CacheKeys.userInfo,
				data: userInfo
			});
			state.userInfo = userInfo;
			state.hasLogin = true;
			uni.switchTab({
				url: '/pages/chat/index'
			});
		},
		logout(state) {
			state.hasLogin = false;
			uni.removeStorageSync(GLOBAL.CacheKeys.userInfo);
			uni.removeStorageSync(GLOBAL.CacheKeys.token);
			uni.navigateTo({
				url:'/pages/login/login'
			})
		}
	}
})

export default store
